import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionsRouter from './modules/permissions'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingsRouter from './modules/settings'
import socialsRouter from './modules/socials'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * 注意：二级菜单仅仅出现在子级路由个数大于等于1个的情况下
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果为true，则不会显示在侧边栏菜单，默认为 false
 * alwaysShow: true               如果设置为true，他将一直显示
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           重定向地址
 * name:'router-name'             路由的名称，必须要 <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    角色控制
    title: 'title'               标题
    icon: 'svg-name'/'el-icon-x' 图标
    breadcrumb: false            是否显示在面包屑中
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 静态路由 都有的路由
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  {
    path: '/import',
    name: 'import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import/index')
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
// 动态路由 每个人拥有的不一样
export const asyncRoutes = [
  departmentsRouter,
  settingsRouter,
  employeesRouter,
  permissionsRouter,
  attendancesRouter,
  salarysRouter,
  approvalsRouter,
  socialsRouter
]

const createRouter = () => new Router({
  // 路由模式
  // mode: 'history', // require service support
  // 每次路由跳转滚动回顶部
  scrollBehavior: () => ({ y: 0 }),
  // 路由映射
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由方法
export function resetRouter() {
  // 再创建一次路由
  const newRouter = createRouter()
  // 路由重新配置
  router.matcher = newRouter.matcher // reset router
}

export default router
